package com.CGFrame.struts.bean.UserLogin;

import java.util.Iterator;

import javax.servlet.http.HttpSession;

import com.CGFrame.struts.POJOs.Authorityrole;
import com.CGFrame.struts.POJOs.Project;
import com.CGFrame.struts.POJOs.Projectreguserrole;
import com.CGFrame.struts.POJOs.Reguser;
import com.CGFrame.struts.POJOs.Reguserrole;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;

public class CheckRole {

	private int Flag = 0;
	public CheckRole(HttpSession session, int authorityId) {
		try
		{
			ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
			String regUserId = (String)session.getAttribute("RegUserID");
			String projectId = (String)session.getAttribute("ProjectID");
			
			Reguser  reguser = (Reguser)pop.UniqueQuery("Reguser", "regUserId,"+regUserId);
			Project  project = (Project)pop.UniqueQuery("Project", "projectId,"+projectId);
			
		
			Iterator ite_regrolesforAllProject =  reguser.getReguserroles().iterator();
			while ( ite_regrolesforAllProject.hasNext() )
			{
				if ( ((Reguserrole)ite_regrolesforAllProject.next() ).getRole().getCanViewAllPrj()==1)// if ViewAllProject
				{
					Flag = 2;
					return;
				}
			}
			Iterator ite_proregroles = project.getProjectreguserroles().iterator();
			while (ite_proregroles.hasNext())
			{
				Projectreguserrole proRegRole = (Projectreguserrole)ite_proregroles.next();
				Reguserrole regrole = proRegRole.getReguserrole();
				if ( regrole.getReguser().getRegUserId().compareTo(reguser.getRegUserId()) == 0 )
				{
					Iterator ite_aurole = regrole.getRole().getAuthorityroles().iterator();
					while(ite_aurole.hasNext())
					{
						Authorityrole aurole = (Authorityrole)ite_aurole.next();
						if (aurole.getAuthority().getAuthorityId().intValue() == authorityId )
						{
							Flag = 1;
							if(aurole.getManage() == 1)
							{
								Flag=2;
								return;
							}
						}
					}
				}
			}
		}
		catch(Exception e)
		{
			
		}
		
	}
	/**
	 * 
	 * @return int Flag =0 不能访问, 1 浏览权限  ,2 管理权限
	 */
	public int getFlag()
	{
		return Flag;
	}

}
